package com.rykj.qxj.rest;

import com.github.pagehelper.PageInfo;
import com.rykj.qxj.annotation.EnableSystemLog;
import com.rykj.qxj.server.model.entity.MetadataBasic;
import com.rykj.qxj.server.model.entity.MetadataElement;
import com.rykj.qxj.server.model.param.DeleteFieldParams;
import com.rykj.qxj.server.model.param.MetadataParams;
import com.rykj.qxj.server.services.IMetadataManageService;
import com.rykj.qxj.server.util.object.StringUtils;
import com.rykj.qxj.util.BeanValidator;
import com.rykj.qxj.util.RegexUtils;
import com.rykj.qxj.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Log4j
@RestController
@RequestMapping("/v1/metadataManage")
@Api(tags = "元数据管理")
public class MetadataManageController {

    @Autowired
    private IMetadataManageService metadataManageService;

    @PostMapping("/saveMetadataBasic")
    @ApiOperation(value = "创建元数据基本信息", notes = "创建元数据基本信息")
    @EnableSystemLog(value="创建元数据基本信息",fullPath ="/v1/metadataManage/saveMetadataBasic")
    public Result saveMetadataBasic(@RequestBody MetadataParams metadataParams) {
        MetadataBasic metadataBasic = metadataParams.getMetadataBasic();
        BeanValidator.check(metadataBasic);
        try {
            String message = metadataManageService.saveMetadataBasic(metadataParams);
            if (StringUtils.isNotEmpty(message)) {
                return new Result(false, message);
            }
            return new Result(metadataBasic);
        } catch (Exception e) {
            log.error("添加失败", e);
            return new Result(false, e.getMessage());
        }
    }

    @PostMapping("/updateMetadataBasic")
    @ApiOperation(value = "修改元数据基本信息", notes = "修改元数据基本信息")
    @EnableSystemLog(value="修改元数据基本信息",fullPath ="/v1/metadataManage/updateMetadataBasic")
    public Result updateMetadataBasic(@RequestBody MetadataParams metadataParams) {

        BeanValidator.check(metadataParams);
        MetadataBasic metadataBasic = metadataParams.getMetadataBasic();
        if(StringUtils.isNotEmpty(metadataBasic.getTableNotes())){
            if(RegexUtils.checkSpecialCharacters(metadataBasic.getTableNotes())){
                return new Result(false, "表注释不能包含特殊字符");
            }
        }
        if(RegexUtils.checkSpecialCharacters(metadataBasic.getTableName())){
            return new Result(false, "表名不能包含特殊字符");
        }
        if(RegexUtils.checkSpecialCharacters(metadataBasic.getBelongDatabase())){
            return new Result(false, "所属数据库不能包含特殊字符");
        }
       // BeanValidator.check(metadataBasic);
        Integer metadataId = metadataManageService.checkTableName(metadataBasic.getTableName(), metadataBasic.getBelongDatabase());
        if (metadataId != null && metadataBasic.getId() != metadataId) {
            return new Result(false, "表名已存在");
        }
        try {
           String errorMsg =   metadataManageService.updateMetadataBasic(metadataParams);
           if(StringUtils.isNotEmpty(errorMsg)){
               return new Result(false, errorMsg);
           }
            return new Result(true, "修改成功");
        } catch (Exception e) {
            log.error("修改失败", e);
            return new Result(false, e.getMessage());
        }
    }

    @GetMapping("/deleteMetadataBasicById")
    @ApiOperation(value = "根据id删除表基本信息", notes = "根据id删除表基本信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "metadataBasicId", value = "元数据id", required = true, paramType = "query")
    })
    @EnableSystemLog(value="根据id删除表基本信息",fullPath ="/v1/metadataManage/deleteMetadataBasicById")
    public Result deleteMetadataBasicById(@RequestParam(value = "metadataBasicId", required = false) Integer metadataBasicId) {
        try {
            String message = metadataManageService.deleteMetadataBasicById(metadataBasicId);
            if(StringUtils.isNotEmpty(message)){
                return new Result(false, message);
            }
            return new Result(true, "删除成功");
        } catch (Exception e) {
            log.error("删除失败", e);
            return new Result(false, e.getMessage());
        }
    }


    @PostMapping("/saveMetadataElement")
    @ApiOperation(value="创建元数据字段信息",notes ="创建元数据字段信息")
    @EnableSystemLog(value="创建元数据字段信息",fullPath ="/v1/metadataManage/saveMetadataElement")
    public Result saveMetadataElement(@RequestBody MetadataParams  metadataParams){
        List<MetadataElement> metadataElements = metadataParams.getMetadataElements();
        BeanValidator.check(metadataElements);
        if(CollectionUtils.isEmpty(metadataElements)){
            return new Result(false,"新增字段为空");
        }
        try{
            String message =  metadataManageService.saveMetadataElement(metadataParams);
            if(StringUtils.isNotEmpty(message)){
                return new Result(false,message);
            }
            return new Result(metadataParams.getMetadataElements());
        }catch (Exception e){
            log.error("添加失败", e);
            return new Result(false, e.getMessage());
        }
    }

    @PostMapping("/updateMetadataElement")
    @ApiOperation(value="修改元数据字段",notes ="修改/删除元数据字段")
    @EnableSystemLog(value="修改元数据字段",fullPath ="/v1/metadataManage/updateMetadataElement")
    public Result updateMetadataElement(@RequestBody MetadataParams metadataParams){
        //Todo同一张表校验字段不能有重复
        try{
            BeanValidator.check(metadataParams.getMetadataElements());
            String errorMsg =  metadataManageService.updateMetadataElement(metadataParams);
            if(StringUtils.isNotEmpty(errorMsg)){
                return new Result(false, errorMsg);
            }
            return new Result(true,"修改成功");
        }catch (Exception e){
            log.error("修改失败", e);
            return new Result(false, e.getMessage());
        }
    }

    @GetMapping("/getMetadataBasicList")
    @ApiOperation(value = "分页获取元数据列表", notes = "分页获取元数据列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页数", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "显示行数", required = true, paramType = "query"),
            @ApiImplicitParam(name = "tableName", value = "表名", paramType = "query"),
            @ApiImplicitParam(name = "tableNotes", value = "表注释", paramType = "query"),
            @ApiImplicitParam(name = "classification", value = "数据分类", paramType = "query"),
            @ApiImplicitParam(name = "directory", value = "数据目录", paramType = "query"),
            @ApiImplicitParam(name = "auditStatus", value = "审核状态,待审核auditing,审核通过pass,审核驳回reject", paramType = "query"),
            @ApiImplicitParam(name = "belongDatabase", value = "所属数据库", paramType = "query")
    })
    public Result getMetadataBasicList(@RequestParam(value = "pageNum") Integer pageNum,
                                       @RequestParam(value = "pageSize") Integer pageSize,
                                       @RequestParam(value = "tableName", required = false) String tableName,
                                       @RequestParam(value = "tableNotes", required = false) String tableNotes,
                                       @RequestParam(value = "classification", required = false) String classification,
                                       @RequestParam(value = "directory", required = false) String directory,
                                       @RequestParam(value = "auditStatus", required = false) String auditStatus,
                                       @RequestParam(value = "belongDatabase", required = false) String belongDatabase) {
        try {
            PageInfo pager = metadataManageService.getMetadataBasicList(pageNum, pageSize, tableName,tableNotes, classification, directory, belongDatabase,auditStatus);
            return new Result(pager);
        } catch (Exception e) {
            log.error("分页获取元数据列表失败", e);
            return new Result(false, "分页获取元数据列表失败");
        }
    }


    @GetMapping("/getTableClassList")
    @ApiOperation(value = "分页获取元数据和分类列表", notes = "分页获取元数据和分类列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页数", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "显示行数", required = true, paramType = "query"),
            @ApiImplicitParam(name = "directory", value = "数据目录id",required = true, paramType = "query"),
            @ApiImplicitParam(name = "notes", value = "描述名称", paramType = "query"),
            @ApiImplicitParam(name = "grade", value = "等级:ONE 一级，TWO 二级", paramType = "query")
    })
    public Result getTableClassList(@RequestParam(value = "pageNum") Integer pageNum,
                                       @RequestParam(value = "pageSize") Integer pageSize,
                                       @RequestParam(value = "directory") String directory,
                                       @RequestParam(value = "notes", required = false) String notes,
                                       @RequestParam(value = "grade", required = false) String grade
    ) {
        try {
            PageInfo pager = metadataManageService.getTableClassList(pageNum, pageSize, directory,notes,grade);
            return new Result(pager);
        } catch (Exception e) {
            log.error("分页获取元数据和分类列表失败", e);
            return new Result(false, "分页获取元数据和分类列表失败");
        }
    }


    @GetMapping("/getMetadataElementList")
    @ApiOperation(value = "分页获取元数据字段列表", notes = "分页获取元数据字段列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页数", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "显示行数", required = true, paramType = "query"),
            @ApiImplicitParam(name = "tableName", value = "表名", paramType = "query"),
            @ApiImplicitParam(name = "tableId", value = "表id", paramType = "query")
    })
    public Result getMetadataElementList(@RequestParam(value = "pageNum") Integer pageNum,
                                         @RequestParam(value = "pageSize") Integer pageSize,
                                         @RequestParam(value = "tableName", required = false) String tableName,
                                         @RequestParam(value = "tableId", required = false) Integer tableId) {
        try {
            PageInfo pager = metadataManageService.getMetadataElementList(pageNum, pageSize, tableName, tableId);
            return new Result(pager);
        } catch (Exception e) {
            log.error("分页获取元数据字段列表失败", e);
            return new Result(false, "分页获取元数据字段列表失败");
        }
    }

    @GetMapping("/getMetadataBasicId")
    @ApiOperation(value = "获取元数据表基本信息详情", notes = "获取元数据表基本详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "metadataBasicId", value = "表id", paramType = "query")
    })
    public Result getMetadataBasicId(@RequestParam(value = "metadataBasicId", required = false) Integer metadataBasicId) {
        try {
            MetadataBasic metadataBasic = metadataManageService.getMetadataBasicId(metadataBasicId);
            return new Result(metadataBasic);
        } catch (Exception e) {
            log.error("获取元数据表基本信息失败", e);
            return new Result(false, "获取元数据表基本信息失败");
        }
    }

    @PostMapping("/deleteMetadataElementById")
    @ApiOperation(value = "删除元数据字段", notes = "删除元数据字段")
    @EnableSystemLog(value="删除元数据字段",fullPath ="/v1/metadataManage/deleteMetadataElementById")
    public Result deleteMetadataElementById(@RequestBody DeleteFieldParams deleteFieldParams) {
        try {
            String message = metadataManageService.deleteMetadataElementById(deleteFieldParams);
            if(StringUtils.isNotEmpty(message)){
                return new Result(false, message);
            }
            return new Result(true, "删除成功");
        } catch (Exception e) {
            log.error("删除失败", e);
            return new Result(false, e.getMessage());
        }
    }


}
